%% 读sobol采样集
clc;
clear;
warning off
param_name = ["RCPC","CCPC",'LCPC','RAi','CAi','LAi','RVi','CVi','LVi','RA8','CA8','LA8','Rpi','Cpi','Rp9','Cp9','Emaxlefti','Eminlefti','Emaxrighti','Eminrighti'];
param_lb = zeros(1,20);
param_ub = ones(1,20)*10;
param_valueset = csvread("E:\CS\EECP_new\sobol\sobol_seq.csv");
%% 生成弹性函数序列
tc = 1;
Tmax = 0.2+0.15*tc;
t = 0.01:0.01:tc;
tn = t/Tmax;
global Entn
global Emaxleft
global Eminleft
global Emaxright
global Eminright
global Eleft_t
global Eright_t
Entn = 1.55*((tn/0.7).^1.9./(1+(tn/0.7).^1.9))./(1+(tn/1.17).^21.9);
Emaxleft = 6.0;
Eminleft = 0.0082;
Emaxright = 0.00042;
Eminright = 0.00003;
Eleft_t= (Emaxleft-Eminleft)*Entn + Eminleft;
Eright_t = (Emaxright-Eminright)*Entn + Eminright;

%Eleft_t=[Eleft_t(3:end),Eleft_t(1:2)];
%Eright_t=[Eright_t(3:end),Eright_t(1:2)];
%CLV_t = 1./Eleft_t;
%CRV_t = 1./Eright_t;
%% 加载simulink模型
filename = 'PLM_siml_sens'
load_system(filename);
%% 迭代设置
Y = size(param_valueset);
Y= Y(1);
model_output = [];
col_n = 1000
%% 迭代求解输出
for i  = 1:Y
   set_param20(filename,param_valueset(i,:));
   pause(1);
   sim(filename,[0,100]);
   p_average =  simout.Data();
   model_output = [model_output,p_average(end)];
   disp(i)
   % if i == col_n
   %      writematrix(model_output,'sobol\model_output.csv');
   %      pause(1);
   %      model_output = [];
   % elseif 
   if mod(i,col_n)==0 || i == Y
        writematrix(model_output,'sobol\model_output.csv','WriteMode','append');
        pause(1);
        model_output = [];
   end
end
disp("sim end!!!")

%% 保存
save()
%%  单项输出测试
set_param20(filename,param_valueset(1497,:));
pause(1);
sim(filename,[0,100]);
%%
plot(Eright_t)
writematrix(model_output,'sobol\model_output.csv');
%%
%Pao_wavedata = simout1.Data();
%p_average(end)

%% 自定义函数-设置模型参数值
function set_param20(filename,param_obj)
    %RCPC
    set_param([filename,'/人体血液循环系统集中参数模型/RRA'],'Resistance',num2str(param_obj(1)))
    set_param([filename,'/人体血液循环系统集中参数模型/RRV'],'Resistance',num2str(param_obj(1)))
    set_param([filename,'/人体血液循环系统集中参数模型/RL'],'Resistance',num2str(param_obj(1)))
    set_param([filename,'/人体血液循环系统集中参数模型/RLA'],'Resistance',num2str(param_obj(1)))
    set_param([filename,'/人体血液循环系统集中参数模型/RLV'],'Resistance',num2str(param_obj(1)))
    set_param([filename,'/人体血液循环系统集中参数模型/RCA'],'Resistance',num2str(param_obj(1)))
   
    %CCPC
    set_param([filename,'/人体血液循环系统集中参数模型/CRA'],'Capacitance',num2str(param_obj(2)))
    set_param([filename,'/人体血液循环系统集中参数模型/CL'],'Capacitance',num2str(param_obj(2)))
    set_param([filename,'/人体血液循环系统集中参数模型/CLA'],'Capacitance',num2str(param_obj(2)))  
    %LCPC
    set_param([filename,'/人体血液循环系统集中参数模型/LL'],'Inductance',num2str(param_obj(3)))
    %RAi
    set_param([filename,'/人体血液循环系统集中参数模型/R0'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/RA'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/A1/R'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/A2/R'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/A3/R'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/A4/R'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/A5/R'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/A6/R'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/A7/R'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/A14/R'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/A15/R'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/A16/R'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/A17/R'],'Resistance',num2str(param_obj(4)))
    set_param([filename,'/人体血液循环系统集中参数模型/A18/R'],'Resistance',num2str(param_obj(4)))
    %CAi
    set_param([filename,'/人体血液循环系统集中参数模型/C0'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/C1'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/CA'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/A1/C'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/A2/C'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/A3/C'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/A4/C'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/A5/C'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/A6/C'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/A7/C'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/A14/C'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/A15/C'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/A16/C'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/A17/C'],'Capacitance',num2str(param_obj(5)))
    set_param([filename,'/人体血液循环系统集中参数模型/A18/C'],'Capacitance',num2str(param_obj(5)))
    %LAi
    set_param([filename,'/人体血液循环系统集中参数模型/L0'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/LA'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/A1/L'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/A2/L'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/A3/L'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/A4/L'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/A5/L'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/A6/L'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/A7/L'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/A14/L'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/A15/L'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/A16/L'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/A17/L'],'Inductance',num2str(param_obj(6)))
    set_param([filename,'/人体血液循环系统集中参数模型/A18/L'],'Inductance',num2str(param_obj(6)))
    %RVi
    set_param([filename,'/人体血液循环系统集中参数模型/RV'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V1/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V2/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V3/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V4/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V5/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V6/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V7/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V8/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V9/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V10/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V11/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V12/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V13/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V14/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V15/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V16/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V17/R'],'Resistance',num2str(param_obj(7)))
    set_param([filename,'/人体血液循环系统集中参数模型/V18/R'],'Resistance',num2str(param_obj(7)))
    %CVi
    set_param([filename,'/人体血液循环系统集中参数模型/V1/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V2/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V3/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V4/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V5/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V6/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V7/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V8/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V9/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V10/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V11/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V12/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V13/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V14/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V15/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V16/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V17/C'],'Capacitance',num2str(param_obj(8)))
    set_param([filename,'/人体血液循环系统集中参数模型/V18/C'],'Capacitance',num2str(param_obj(8)))
    %LVi
    set_param([filename,'/人体血液循环系统集中参数模型/LV'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V1/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V2/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V3/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V4/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V5/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V6/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V7/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V8/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V9/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V10/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V11/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V12/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V13/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V14/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V15/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V16/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V17/L'],'Inductance',num2str(param_obj(9)))
    set_param([filename,'/人体血液循环系统集中参数模型/V18/L'],'Inductance',num2str(param_obj(9)))
    %RA8
    set_param([filename,'/人体血液循环系统集中参数模型/A8/R'],'Resistance',num2str(param_obj(10)))
    set_param([filename,'/人体血液循环系统集中参数模型/A9/R'],'Resistance',num2str(param_obj(10)))
    set_param([filename,'/人体血液循环系统集中参数模型/A10/R'],'Resistance',num2str(param_obj(10)))
    set_param([filename,'/人体血液循环系统集中参数模型/A11/R'],'Resistance',num2str(param_obj(10)))
    set_param([filename,'/人体血液循环系统集中参数模型/A12/R'],'Resistance',num2str(param_obj(10)))
    set_param([filename,'/人体血液循环系统集中参数模型/A13/R'],'Resistance',num2str(param_obj(10)))
    %CA8
    set_param([filename,'/人体血液循环系统集中参数模型/A8/C'],'Capacitance',num2str(param_obj(11)))
    set_param([filename,'/人体血液循环系统集中参数模型/A9/C'],'Capacitance',num2str(param_obj(11)))
    set_param([filename,'/人体血液循环系统集中参数模型/A10/C'],'Capacitance',num2str(param_obj(11)))
    set_param([filename,'/人体血液循环系统集中参数模型/A11/C'],'Capacitance',num2str(param_obj(11)))
    set_param([filename,'/人体血液循环系统集中参数模型/A12/C'],'Capacitance',num2str(param_obj(11)))
    set_param([filename,'/人体血液循环系统集中参数模型/A13/C'],'Capacitance',num2str(param_obj(11)))
    %LA8
    set_param([filename,'/人体血液循环系统集中参数模型/A8/L'],'Inductance',num2str(param_obj(12)))
    set_param([filename,'/人体血液循环系统集中参数模型/A9/L'],'Inductance',num2str(param_obj(12)))
    set_param([filename,'/人体血液循环系统集中参数模型/A10/L'],'Inductance',num2str(param_obj(12)))
    set_param([filename,'/人体血液循环系统集中参数模型/A11/L'],'Inductance',num2str(param_obj(12)))
    set_param([filename,'/人体血液循环系统集中参数模型/A12/L'],'Inductance',num2str(param_obj(12)))
    set_param([filename,'/人体血液循环系统集中参数模型/A13/L'],'Inductance',num2str(param_obj(12)))
    %Rpi
    set_param([filename,'/人体血液循环系统集中参数模型/P14/Ra'],'Resistance',num2str(param_obj(13)))
    set_param([filename,'/人体血液循环系统集中参数模型/P16/Ra'],'Resistance',num2str(param_obj(13)))
    set_param([filename,'/人体血液循环系统集中参数模型/P2/Ra'],'Resistance',num2str(param_obj(13)))
    set_param([filename,'/人体血液循环系统集中参数模型/P5/Ra'],'Resistance',num2str(param_obj(13)))
    set_param([filename,'/人体血液循环系统集中参数模型/P6/Ra'],'Resistance',num2str(param_obj(13)))
    set_param([filename,'/人体血液循环系统集中参数模型/P7/Ra'],'Resistance',num2str(param_obj(13)))
    set_param([filename,'/人体血液循环系统集中参数模型/P14/Rv'],'Resistance',num2str(param_obj(13)))
    set_param([filename,'/人体血液循环系统集中参数模型/P16/Rv'],'Resistance',num2str(param_obj(13)))
    set_param([filename,'/人体血液循环系统集中参数模型/P2/Rv'],'Resistance',num2str(param_obj(13)))
    set_param([filename,'/人体血液循环系统集中参数模型/P5/Rv'],'Resistance',num2str(param_obj(13)))
    set_param([filename,'/人体血液循环系统集中参数模型/P6/Rv'],'Resistance',num2str(param_obj(13)))
    set_param([filename,'/人体血液循环系统集中参数模型/P7/Rv'],'Resistance',num2str(param_obj(13)))
    %Cpi
    set_param([filename,'/人体血液循环系统集中参数模型/P14/C'],'Capacitance',num2str(param_obj(14)))
    set_param([filename,'/人体血液循环系统集中参数模型/P16/C'],'Capacitance',num2str(param_obj(14)))
    set_param([filename,'/人体血液循环系统集中参数模型/P2/C'],'Capacitance',num2str(param_obj(14)))
    set_param([filename,'/人体血液循环系统集中参数模型/P5/C'],'Capacitance',num2str(param_obj(14)))
    set_param([filename,'/人体血液循环系统集中参数模型/P6/C'],'Capacitance',num2str(param_obj(14)))
    set_param([filename,'/人体血液循环系统集中参数模型/P7/C'],'Capacitance',num2str(param_obj(14)))
    %Rp9
    set_param([filename,'/人体血液循环系统集中参数模型/P9/Ra'],'Resistance',num2str(param_obj(15)))
    set_param([filename,'/人体血液循环系统集中参数模型/P11/Ra'],'Resistance',num2str(param_obj(15)))
    set_param([filename,'/人体血液循环系统集中参数模型/P13/Ra'],'Resistance',num2str(param_obj(15)))
    set_param([filename,'/人体血液循环系统集中参数模型/P9/Rv'],'Resistance',num2str(param_obj(15)))
    set_param([filename,'/人体血液循环系统集中参数模型/P11/Rv'],'Resistance',num2str(param_obj(15)))
    set_param([filename,'/人体血液循环系统集中参数模型/P13/Rv'],'Resistance',num2str(param_obj(15)))
    %Cp9
    set_param([filename,'/人体血液循环系统集中参数模型/P9/C'],'Capacitance',num2str(param_obj(16)))
    set_param([filename,'/人体血液循环系统集中参数模型/P11/C'],'Capacitance',num2str(param_obj(16)))
    set_param([filename,'/人体血液循环系统集中参数模型/P13/C'],'Capacitance',num2str(param_obj(16)))
    % 'Emaxlefti','Eminlefti','Emaxrighti','Eminrighti'
    % global Entn
    % global Emaxleft
    % global Eminleft
    % global Emaxright
    % global Eminright
    % global Eleft_t
    % global Eright_t
    % Emaxleft = param_obj(17);
    % Eminleft = param_obj(18);
    % Emaxright = param_obj(19);
    % Eminright = param_obj(20);
    % Eleft_t= (Emaxleft-Eminleft)*Entn + Eminleft;
    % Eright_t = (Emaxright-Eminright)*Entn + Eminright;

end

